﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WinCustomsService.Helper
{
  


    public class NewSign311Helper
    {
        public string initData = String.Empty;

        public string getSignDataNoHashAsPEMParameter()
        {
            
        }
        public string GetDynamicSignDataParameter(XmlDocument document)
        {
            return GetSignatureValueBeforeSend(document);
        }
        public string GetSignatureValueBeforeSend(XmlDocument document)
        {
            CreateSignatureElement(document);
            var nodeList = document.GetElementsByTagName("ds:SignedInfo");
            var node = nodeList.Item(0);
            return node.OuterXml;
        }

        public void CreateSignatureElement(XmlDocument document)
        {
            String testNamespace = "http://www.testnamespace/vs/";
            XmlElement canonicalizationMethodElement = document.CreateElement("ds", "CanonicalizationMethod", testNamespace); //添加person节点
            canonicalizationMethodElement.SetAttribute("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
            canonicalizationMethodElement.InnerText = string.Empty;
            XmlElement signatureMethodElement = document.CreateElement("ds", "SignatureMethod", testNamespace);
            signatureMethodElement.SetAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
            signatureMethodElement.InnerText = string.Empty;

            XmlElement transformElement = document.CreateElement("ds", "Transform", testNamespace);
            transformElement.SetAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature");
            transformElement.InnerText = string.Empty;

            XmlElement transformsElement = document.CreateElement("ds:Transforms", testNamespace);
            transformsElement.AppendChild(transformElement);

            string digestValue = GetDigestValueOfCEBXxxMessage();


            //初始化摘要信息
            XmlElement digestValueElement = document.CreateElement("ds", "DigestValue", testNamespace);
            digestValueElement.InnerText = digestValue;

            XmlElement digestMethodElement = document.CreateElement("ds", "DigestMethod", testNamespace);
            digestMethodElement.SetAttribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
            XmlElement referenceElement = document.CreateElement("ds", "Reference", testNamespace);
            referenceElement.SetAttribute("URI", string.Empty);
            referenceElement.AppendChild(transformsElement);
            referenceElement.AppendChild(digestMethodElement);
            referenceElement.AppendChild(digestValueElement);

            XmlElement signedInfoElement = document.CreateElement("ds", "SignedInfo", testNamespace);
            signedInfoElement.SetAttribute("xmlns:ceb", "http://www.chinaport.gov.cn/ceb");
            signedInfoElement.SetAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
            signedInfoElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

            signedInfoElement.AppendChild(canonicalizationMethodElement);
            signedInfoElement.AppendChild(signatureMethodElement);
            signedInfoElement.AppendChild(referenceElement);

            XmlElement signatureValueElement = document.CreateElement("ds", "SignatureValue", testNamespace);
            signatureValueElement.InnerText = "signatureValue";

            XmlElement x509Certificate = document.CreateElement("ds", "X509Certificate", testNamespace);
            x509Certificate.InnerText = string.Empty;

            XmlElement x509DataElement = document.CreateElement("ds", "X509Data", testNamespace);
            x509DataElement.AppendChild(x509Certificate);

            XmlElement keyNameElement = document.CreateElement("ds", "KeyName", testNamespace);
            keyNameElement.InnerText = string.Empty;

            XmlElement keyInfoElement = document.CreateElement("ds", "KeyInfo", testNamespace);
            keyInfoElement.AppendChild(keyNameElement);
            keyInfoElement.AppendChild(x509DataElement);

            XmlElement signatureElement = document.CreateElement("ds", "Signature", testNamespace);
            signatureElement.SetAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
            signatureElement.AppendChild(signedInfoElement);
            signatureElement.AppendChild(signatureValueElement);
            signatureElement.AppendChild(keyInfoElement);



            document.DocumentElement.AppendChild(signatureElement);

        }

        public  string GetDigestValueOfCEBXxxMessage() 
        {
            string newxml = XmlHelper.XmlDsigC14NTransform(initData);
            return XmlHelper.Sha1Signature(newxml, Encoding.UTF8); // sh1 转string64
        }
    }
}
